package com.cn.qgx428.lazydiary.ui.fragment


import android.content.Context
import android.media.Image
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.ImageView
import android.widget.TextView

import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.adapter.RAdapter
import com.cn.qgx428.lazydiary.adapter.RViewHolder
import com.cn.qgx428.lazydiary.extension.getCurrenMouth
import com.cn.qgx428.lazydiary.extension.getCurrenMouthWidthYear
import com.cn.qgx428.lazydiary.mWidget.SquareLayout
import com.cn.qgx428.lazydiary.ui.activity.StickNoteActivity
import com.cn.qgx428.lazydiary.ui.activity.BillActivity
import com.cn.qgx428.lazydiary.ui.activity.DiaryActivity
import com.cn.qgx428.lazydiary.ui.activity.PlanActivity
import de.hdodenhof.circleimageview.CircleImageView
import kotlinx.android.synthetic.main.fragment_function.*
import org.jetbrains.anko.startActivity
import java.util.*


/**
 * A simple [Fragment] subclass.
 */
class FunctionFragment : BaseFragment() {

    override fun getLayoutId()= R.layout.fragment_function

    override fun initView() {
        var adapter = TitleAdapter(context, R.layout.adapter_functionf_re, mutableListOf("计划","日记","便利贴","账单"))
        functionf_re.adapter = adapter
        functionf_re.layoutManager = GridLayoutManager(context, 2)
    }

    private class TitleAdapter(context: Context, layoutId: Int, datas: MutableList<String>) : RAdapter<String>(context, layoutId, datas) {

        override fun conver(holder: RViewHolder, data: String) {
            val title : TextView = holder.getView(R.id.functionf_re_item_title)!!
            title.setText(data as String)
            val layout: SquareLayout = holder.getView(R.id.functinof_re_item_layout)!!
            val icon: ImageView = holder.getView(R.id.functino_re_item_realicon)!!

            layout.setTag(data as String)
            layout.setOnClickListener({
                when(layout.getTag().toString()){
                    "便利贴" -> mContext.startActivity<StickNoteActivity>()
                    "账单" -> mContext.startActivity<BillActivity>(
                        MConst.CURRENT_MOUTH_WITH_YEAR to Calendar.getInstance().getCurrenMouthWidthYear()
                    )
                    "计划" -> mContext.startActivity<PlanActivity>()
                    "日记" -> mContext.startActivity<DiaryActivity>()
                }
//                if (layout.getTag().toString().equals("便利贴"))
//                    mContext.startActivity<StickNoteActivity>()
            })

            icon.setImageResource(getIconResouceId(data))
        }

        private fun getIconResouceId(type: String): Int{
            when(type){
                "便利贴"-> return R.drawable.ic_stikynote_white
                "账单" -> return R.drawable.ic_bill_white
                "计划" -> return R.drawable.ic_clock_white
                "日记" -> return R.drawable.ic_book_white2
            }

            return R.mipmap.ic_launcher
        }
    }

}// Required empty public constructor
